package com.ruoyi.web.controller.process;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.process.ProcessInventory;
import com.ruoyi.system.domain.process.ProcessRibbon;
import com.ruoyi.system.domain.process.ProcessUnusual;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.processService.IProcessInventoryService;
import com.ruoyi.system.service.processService.IProcessUnusualService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 镀铝分卷加工异常Controller
 * 
 * @author ljx
 * @date 2020-07-15
 */
@Controller
@RequestMapping("/process/unusual")
public class ProcessUnusualController extends BaseController
{
    private String prefix = "process/unusual";

    @Autowired
    private IProcessUnusualService processUnusualService;
    @Resource
    private ISysUserService iSysUserService;

    @RequiresPermissions("process:unusual:view")
    @GetMapping()
    public String unusual(ModelMap mmap)
    {
        List<SysUser> users = iSysUserService.getAllUser();
        mmap.put("users",users);
        return prefix + "/unusual";
    }

    /**
     * 查询镀铝分卷加工异常列表
     */
    @RequiresPermissions("process:unusual:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProcessUnusual processUnusual)
    {
        startPage();
        List<ProcessUnusual> list = processUnusualService.selectProcessUnusualList(processUnusual);
        return getDataTable(list);
    }

    /**
     * 导出镀铝分卷加工异常列表
     */
    @RequiresPermissions("process:unusual:export")
    @Log(title = "镀铝分卷加工异常", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProcessUnusual processUnusual)
    {
        List<ProcessUnusual> list = processUnusualService.selectProcessUnusualList(processUnusual);
        ExcelUtil<ProcessUnusual> util = new ExcelUtil<ProcessUnusual>(ProcessUnusual.class);
        return util.exportExcel(list, "unusual");
    }

    /**
     * 新增镀铝分卷加工异常
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //并卷编号列表只能选择镀铝和分卷的
        List<ProcessRibbon> ribbons = processUnusualService.getRibbonsNew();
        List<SysUser> users = iSysUserService.getAllUser();
        mmap.put("users",users);
        mmap.put("ribbons",ribbons);
        return prefix + "/add";
    }

    /**
     * 新增保存镀铝分卷加工异常
     */
    @RequiresPermissions("process:unusual:add")
    @Log(title = "镀铝分卷加工异常", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProcessUnusual processUnusual)
    {
        processUnusual.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(processUnusualService.insertProcessUnusualNew(processUnusual));
    }

    /**
     * 修改镀铝分卷加工异常
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ProcessUnusual processUnusual = processUnusualService.selectProcessUnusualById(id);
        List<SysUser> users = iSysUserService.getAllUser();
        //List<ProcessInventory> inventories = processUnusualService.selectInventoryByUnusualId(id);
        mmap.put("users",users);
        //mmap.put("inventorys",inventories);
        mmap.put("processUnusual", processUnusual);
        return prefix + "/edit";
    }

    /**
     * 修改保存镀铝分卷加工异常
     */
    @RequiresPermissions("process:unusual:edit")
    @Log(title = "镀铝分卷加工异常", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProcessUnusual processUnusual)
    {
        processUnusual.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(processUnusualService.updateProcessUnusual(processUnusual));
    }

    /**
     * 删除镀铝分卷加工异常
     */
    @RequiresPermissions("process:unusual:remove")
    @Log(title = "镀铝分卷加工异常", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(processUnusualService.deleteProcessUnusualByIds(ids));
    }
}
